import {
	userIndex
} from 'utils/api/user.js'
import {
	orderPageList,
	changeStatus
} from 'utils/api/order.js'
export default {
	data() {
		return {
			tabIndex:0,
			postData: {
				status: '',
				page: 1,
				keywords: ''
			},
			loadmore:false,
			orderList: [],
			userInfo: {},
			total: 0,
			checkType: 'loginout',
			checkItem: {},
		}
	},
	onShow() {
		this.getList();
		this.getInfo();
	},
	onPullDownRefresh() {
		this.postData.page = 1;
		this.getList()
	},
	methods: {
		// 获取用户信息
		getInfo() {
			userIndex().then((res) => {
				this.userInfo = res.data
			})
		},
		openDialog(key, item) {
			this.checkItem = item
			this.checkType = key
			this.$refs.tipsPop.open()
		},
		ontabchange(e) {
			let index = e.target.current || e.detail.current;
			this.tabIndex = index;
			this.checkTab(this.tabArr[index])
		},
		checkTab(item) {
			this.postData.status = item.id
			this.searchData()
		},
		// 搜索
		searchData() {
			this.postData.page = 1;
			this.getList();
		},
		scrollToLower(){
			if(this.orderList.length<this.total){
				this.postData.page+=1;
				this.getList()
			}
		},
		// 获取数据
		getList(){
			if(this.postData.page==1){
				uni.showLoading({
				  title: '加载中',
				  mask: true
				})
			}
			orderPageList(this.postData).then((res)=>{
				uni.hideLoading();
				this.total = res.data.total;
				if (res.data.list.length !== 0) {
				  this.loadmore = false;
				}
				if (this.postData.page == 1) {
				  this.orderList = res.data.list;
				} else {
				  this.orderList = this.orderList.concat(res.data.list);
				}
				uni.stopPullDownRefresh();
				setTimeout(() => {
					this.loadmore = true;
				}, 800)
			})
		},
		saveData(respone) {
			if(this.checkType === 'loginout') {
				uni.removeStorageSync('token')
				uni.switchTab({
					url: '/pages/home/index'
				})
			} else {
				const questData = {
					'pickup': { api: changeStatus, data: {status: '2',id: this.checkItem.id} },
					'checkDr': { api: changeStatus, data: {status: '3',id: this.checkItem.id, driverId: respone } },
					
					// 驾驶员
					'goods': { api: changeStatus, data: {status: '3',id: this.checkItem.id} },
					'transport': { api: changeStatus, data: {status: '4',id: this.checkItem.id} },
					'over': { api: changeStatus, data: {status: '5',id: this.checkItem.id} },
				}
				uni.showLoading({
					title: '保存中',
					mask: true
				})
				const dataType = questData[this.checkType]
				dataType.api(dataType.data).then((res) => {
					uni.hideLoading()
					uni.showToast({ title: '操作成功', icon: 'none'})
					setTimeout(() => {
						this.getList()
					}, 600)
				})
			}
		},
	}
}